package com.ask.controller.user;

import com.ask.entity.Dish;
import com.ask.entity.SetMeal;
import com.ask.result.Result;
import com.ask.service.SetMealService;
import com.ask.vo.DishItemVO;
import com.ask.vo.DishVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userSetMealController")
@RequestMapping("/user/setmeal")
public class SetMealController {

    private static final Logger log = LoggerFactory.getLogger(SetMealController.class);
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetMealService setMealService;

    /**
     * 根据分类条件查询套餐
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setMealCache", key = "#categoryId")
    public Result<List<SetMeal>> listByCategoryId(Long categoryId) {
        // 1. 查询redis, 存在直接返回
//        String key = RedisConstant.CACHE_SETMEAL_KEY + categoryId;
//        List<SetMeal> list = (List<SetMeal>) redisTemplate.opsForValue().get(key);
//        if (list != null && list.size() > 0)
//            return Result.success(list);

        // 2. 不存在查询，查询数据库
        log.info("query setMeal, categoryId: {}", categoryId);
        List<SetMeal> list = setMealService.listByCategoryId(categoryId);
        // 3. 查询结果存储至redis
//        redisTemplate.opsForValue().set(key, list);
        // 4. 返回
        return Result.success(list);
    }


    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>> getDishesById(@PathVariable Long id) {
        List<DishItemVO> dishVOList = setMealService.getDishesById(id);
        return Result.success(dishVOList);
    }
}
